package com.question.service;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import com.achievement.dao.SubjectRepository;
import com.achievement.entity.Subject;
import com.common.util.TablePage;
import com.paper.dao.SubtopicsRepository;
import com.question.dao.MultipleRepository;
import com.question.dao.OptionsRepository;
import com.question.entity.Multiple;
import com.question.entity.Options;
import com.question.entity.Single;
import com.question.entity.dto.MultipleDTO;

@Service
@Transactional
public class MultipleService implements IMultipleService {
	@Autowired
	private MultipleRepository multipleDao;
	@Autowired
	private SubjectRepository subjectDao;
	@Autowired
	private OptionsRepository optionsDao;
	@Autowired
	private SubtopicsRepository subtopicsDao;

	public List<MultipleDTO> findAll() {
		List<MultipleDTO> result = new ArrayList<MultipleDTO>();
		List<Multiple> list = (List<Multiple>) multipleDao.findAll();
		for(Multiple m:list) {
			String subject = "";
			if(m.getSubject()!=null)subject = m.getSubject().getName();
			result.add(new MultipleDTO(m, subject));
		}
		return result;
	}

	public void saveOrUpdate(Multiple multiple,String[] answerKey) {
		if(null==multiple.getId()){
			//新增
			Subject subject = new Subject();
			if(null!=multiple.getSubject()&&null!=multiple.getSubject().getId())
				subject = subjectDao.findOne(multiple.getSubject().getId());
			multiple.setSubject(subject);
			
			//答案选项编号
			List<String> answerCode = new ArrayList<String>();
			//1 保存选项
			Map<String, Options> options = multiple.getOptions();
			for(String s:options.keySet()) {
				String code = UUID.randomUUID().toString();
				options.get(s).setNum(code);
				for(int i=0;i<answerKey.length;i++) {
					if(answerKey[i].equals(s))
						answerCode.add(code);
				}
				optionsDao.save(options.get(s));
				options.put(s, optionsDao.findByCode(code));
			}
			multiple.setOptions(options);
			
			Set<Options> answers = new HashSet<Options>();
			for(String s:answerCode) {
				answers.add(optionsDao.findByCode(s));
			}
			multiple.setAnswers(answers);
			
			multipleDao.save(multiple);
		}else {
			
		}
	}

	public void delete(Long id) {
		multipleDao.delete(id);
	}

	public TablePage<MultipleDTO> findPage(Specification<Multiple> spec, Pageable pageable) {
		Page<Multiple> page = multipleDao.findAll(spec, pageable);
		List<MultipleDTO> list = new ArrayList<MultipleDTO>();
		for(Multiple m:page.getContent()) {
			list.add(new MultipleDTO(m, m.getSubject().getName()));
		}
		Page<MultipleDTO> result = new PageImpl<>(list, pageable, page.getTotalElements());
		return TablePage.getTablePage(result);
	}

	@Override
	public TablePage<Multiple> findRealPage(Specification<Multiple> spec, Pageable pageable) {
		// TODO Auto-generated method stub
		return TablePage.getTablePage(multipleDao.findAll(spec, pageable));
	}

	@Override
	public Multiple findOne(Long id) {
		// TODO Auto-generated method stub
		return multipleDao.findOne(id);
	}

	@Override
	public boolean isNumExist(Multiple multiple) {
		// TODO Auto-generated method stub
		Multiple s = multipleDao.findByNum(multiple.getNum());
		if(null!=s&&s.getId()!=multiple.getId())return true;
		return false;
	}

	@Override
	public boolean isUsing(Long id) {
		// TODO Auto-generated method stub
		if(subtopicsDao.findByMultipleId(id).size()>0)
			return true;
		return false;
	}

}
